#ifndef _SHUXIE_H_
#define _SHUXIE_H_

/* C interface */
#ifdef __cplusplus
#include <cstdio>
#else
#include <stdio.h>
#endif

#ifdef __cplusplus
extern "C" {
#endif

/*文件数据头*/
typedef  struct fileHead{
    char magic[4];//魔数
    short ver;//版本
    short width;//宽
    short height;//高
}FILEHEAD;

typedef struct shuxie_recognizer_t shuxie_recognizer_t;//识别
typedef struct shuxie_character_t shuxie_character_t;//单字
typedef struct shuxie_resultword_t shuxie_resultword_t;//结果
typedef struct shuxie_stroke_t shuxie_stroke_t;//笔画


extern shuxie_stroke_t* shuxie_character_getat(shuxie_character_t *character,int strokeid);//获取字中笔画项
extern int shuxie_stroke_size(shuxie_stroke_t* stroke);//笔画中点数
extern void shuxie_stroke_destroy(shuxie_stroke_t *stroke);
extern short shuxie_stroke_getat_x(shuxie_stroke_t *stroke,int pointid);//读取笔画中点
extern short shuxie_stroke_getat_y(shuxie_stroke_t *stroke,int pointid);

extern shuxie_character_t*  shuxie_character_new();
extern void shuxie_character_clear(shuxie_character_t *character);
extern void shuxie_character_initSize(shuxie_character_t *character,int width, int height);
extern int shuxie_character_addPoint(shuxie_character_t *character,int strokeId, int x, int y);
extern void shuxie_character_destroy(shuxie_character_t *character);
extern int  shuxie_character_strokeCount(shuxie_character_t *character);//笔画数
extern void shuxie_character_setword(shuxie_character_t *character,const char* word);//关联字符串

extern shuxie_recognizer_t*  shuxie_recognizer_new();
extern void shuxie_recognizer_destroy(shuxie_recognizer_t *recognizer);
extern int shuxie_recognizer_loadModel(shuxie_recognizer_t *recognizer,const char* filePath,int bUser);//载入字模 bUser=0系统 bUser=1用户
extern int shuxie_recognizer_loadXML(shuxie_recognizer_t *recognizer,const char* filePath);//载入字模
extern int shuxie_recognizer_saveModel(shuxie_recognizer_t *recognizer,const char* filePath);//保存字模 保存bUser!=0字模
extern void shuxie_recognizer_addCharacter(shuxie_recognizer_t *recognizer,shuxie_character_t *character);//追加字模
extern void shuxie_recognizer_reCharacter(shuxie_recognizer_t *recognizer,shuxie_character_t *character);//追加字模
extern void shuxie_recognizer_addUserCharacter(shuxie_recognizer_t *recognizer,shuxie_character_t *character);//追加用户字模
extern shuxie_character_t * shuxie_recognizer_getat(shuxie_recognizer_t *recognizer,int characterid);//获取字库中字模

extern shuxie_resultword_t* shuxie_toword_dwt(shuxie_recognizer_t* recognizer,shuxie_character_t *character,int wordcount);//识别
extern shuxie_resultword_t* shuxie_toword_knn(shuxie_recognizer_t* recognizer,shuxie_character_t *character,int wordcount);//识别

extern shuxie_resultword_t *shuxie_resultword_new() ;
extern void shuxie_resultword_destroy(shuxie_resultword_t *resultword);
extern int shuxie_resultword_count(shuxie_resultword_t *resultword);//字个数
extern char* shuxie_resultword_at(shuxie_resultword_t *resultword,int index);//读取值

#ifdef __cplusplus
}
#endif

#ifdef __cplusplus


#include <stdio.h>
#include <string>
#include <vector>

#include <iostream>
#include <algorithm>


namespace HandWriteRecognizer {

enum recognizerType{
        KNN,
        DWT,
    };

    struct Point{
        short x;//X
        short y;//Y
        short direction;//方向3.14159*100
    };//坐标结构 点

    struct Stroke{
        std::vector<Point> points;
    };//单笔结构 线条

    struct Word{
        std::string word;
        double dist;
        int strokeCount;
    };


    class Character{

    public:
        Character();
        void initSize(int width, int height);
        bool addPoint(int strokeId, int x, int y);
        void clear();

        int lastStrokeId;
        int strokeCount;
        int width, height;
        std::string word;
        std::vector<Stroke> strokes;
        int bUser;//0系统，1用户

    };//字特征

class Recognizer{

    public:
        Recognizer();
        bool loadModelFile(const char* filePath,int bUser);//载入字模
        bool openFile(const char *file);//打开字板
        void clear();
        bool saveModelFile(const char* filePath);//保存字模
        bool loadXmlFile(const std::string& xmlfile);//载入字模ＸＭＬ文件
        bool recognize(Character *character, std::vector<std::string> &resultWords, int count,recognizerType retype);//识别
        void addCharacter(Character *character);//追加字模
        void reCharacter(Character *character);//更新字模
        int getDirection(Character *character);//计算方向值的和
        Character* getCharacter(int index);//读取字模
        Character* findCharacter(const std::string& word);//查找字模

        std::vector<Character> characters;//特征库
protected:
        void loadfile1(std::ifstream &ifs,int bUser,bool bnorm);//版本1读取
    };

class Result{
public:
    Result();
    void addWord(std::string s);
    void clear();
    int getcount();
    std::string getWord(int i);
private:
    std::vector<std::string> resultWord;
};//结果类

}



#endif

#endif // SHUXIE_H
